<?php

namespace Tbf\Bundle\PackageBundle\RelationPackage;

use Tbf\Bundle\PackageBundle\ActionInterface;
use Tbf\Bundle\PackageBundle\Exception\PackageException;
use Tbf\Bundle\TbfBundle\Controller\TbfController;

/**
 * 列出关系
 * ManyToMany列出关系,第2个必须要有指回第1个的属性,顺序非常重要
 */
class ListAction extends TbfController implements ActionInterface
{
    /** @var  RelationControllerConfig */
    protected $config;
    public function setConfig(RelationControllerConfig $config)
    {
        $this->config = $config;
    }
    public function execute($input)
    {
        if ($this->config->hasPropertyNameToAnother('second')) {
            return $this->executeBaseOnSecondPropertyNameToAnother($input);
        } elseif ($this->config->hasPropertyNameToAnother('first')) {
            return $this->executeBaseOnFirstPropertyNameToAnother($input);
        } else {
            throw new PackageException('listAction: require config of property_name_to_another');
        }
    }

    /**
     * 这搞法可能效率低一些
     */
    public function executeBaseOnFirstPropertyNameToAnother($input)
    {
        $err = $this->checkParameterExist($input,array($this->config->getIdName('first')));
        if ($err!==null) {
            return array(null,$err);
        }
        $qb = $this->getDM()->createQueryBuilder()
            ->from($this->config->getEntityClassName('second'),'second')
            ->from($this->config->getEntityClassName('first'),'first')
            ->join('first.'.$this->config->getPropertyNameToAnother('first'),'second1')
            ->andWhere('second1.id=second.id')
            ->andWhere('first.id=:first_id')
            ->select('second')
            ->setParameter('first_id',$input[$this->config->getIdName('first')]);
        if ($this->config->isListQuery()) {
            $data = $this->get('tbf_database.list_query')->create()
                ->fromOrmQueryBuilder($qb)
                ->fromListQueryArray($this->defaultParameter($input,'list_query',array()))
                ->getPageResult();

        } else {
            $data = $qb->getQuery()->getResult();
        }

        return array($data,null);
    }
    public function executeBaseOnSecondPropertyNameToAnother($input)
    {
        $err = $this->checkParameterExist($input,array($this->config->getIdName('first')));
        if ($err!==null) {
            return array(null,$err);
        }
        $qb = $this->getDM()->createQueryBuilder()
            ->from($this->config->getEntityClassName('second'),'second')
            ->join('second.'.$this->config->getPropertyNameToAnother('second'),'first')
            ->where('first.id=:first_id')
            ->select('second')
            ->setParameter('first_id',$input[$this->config->getIdName('first')]);

        if ($this->config->isListQuery()) {
            $data = $this->get('tbf_database.list_query')->create()
                ->fromOrmQueryBuilder($qb)
                ->fromListQueryArray($this->defaultParameter($input,'list_query',array()))
                ->getPageResult();

        } else {
            $data = $qb->getQuery()->getResult();
        }

        return array($data,null);
    }

}
